package com.usoft.sso.grpc.bl;

import com.google.protobuf.InvalidProtocolBufferException;
import com.usoft.sso.common.ProtoBufUtil;
import com.usoft.sso.common.VerifyParamsUtil;
import com.usoft.sso.dal.entity.User;
import com.usoft.sso.dal.mapper.UserMapper;
import com.usoft.sso.grpc.api.entity.UserInfo;
import com.usoft.sso.grpc.api.protobuf.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 用户领域 业务逻辑层
 *
 * @author: wangcanyi
 * @date: 2018-07-27 10:44
 **/
@Service
public class UserBl{
	@Autowired
	private UserMapper userMapper;


	/**
	 * 获取用户信息
	 *
	 * @param request
	 * @return
	 */
	public GetUserInfoResponse.Builder getUserInfo(GetUserInfoRequest request) throws InvalidProtocolBufferException {
		GetUserInfoResponse.Builder response = GetUserInfoResponse.newBuilder();
		VerifyParamsUtil.hasText(request.getUserCode(), "用户编号不能为空");
		User user = userMapper.findByUserCode(request.getUserCode());
		if (user == null) {
			return GetUserInfoResponse.newBuilder();
		}
		UserInfo.Builder uiBuilder = UserInfo.newBuilder();
		uiBuilder = ProtoBufUtil.toProtoBuf(uiBuilder, user);
		response.setUserInfo(uiBuilder);
		return response;
	}



}
